﻿@model News
@using Models
@{
    var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    FormsAuthenticationTicket auth = null;
    if (cookie != null)
    {
        auth = FormsAuthentication.Decrypt(cookie.Value);

        ViewBag.auth = auth;
    }

    ViewBag.Title = "Add news";
}
<link href="/Content/start/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css"/>
  <script src="/Scripts/jquery-1.7.2.min.js"></script>
  <script src="/Scripts/jquery-ui-1.8.21.custom.min.js"></script>
<style>
	#sortable1, #sortable2 { list-style-type: none; margin: 0; padding: 0 0 2.5em; float: left; margin-right: 10px; }
	#sortable1 li, #sortable2 li { margin: 0 5px 5px 5px; padding: 5px; font-size: 1.2em; width: 120px; }
	</style>
	<script>
	   $(function () {
	      $("#sortable1, #sortable2").sortable(
	            {
	               remove: function () {
	                  var tags = document.getElementById('tags');
	                  tags.value = "";
	                  var ui = document.getElementById('sortable2');
	                  var liNodes = ui.getElementsByTagName('li');
	                  for (var i = 0; i < liNodes.length; i++) {
	                     if (tags.value == "")
	                        tags.value = liNodes[i].id;
	                     else {
	                        tags.value += ',' + liNodes[i].id;
	                     }


	                  }
	                  // alert(tags.value);
	               },
	               connectWith: '.otherlist'
	            },
	            {
	               dropOnEmpty: true
	            },
	            { scroll: false }


	    );
	   });
	    var currentFielsIndex = 1;
        function addField() {
            currentFielsIndex++;
            var file = document.createElement("div");
            file.innerHTML = "<input name=\"uploadFiles\" type=\"file\" /> <a onclick=\"return deleteField(this)\" href=\"#\">[X]</a><br/>";
            document.getElementById("filesToUpload").appendChild(file);
            return false;
        }

        function deleteField(a) {
            var div = a.parentNode;
            
            div.innerHTML = "";
            return false;
        }

	</script>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

<div style="float: left; height: 70px"><a href="/admin/index" ><img src="/Content/images/back.png" /></a></div>
<div style="float: left; margin-left: 15px; text-align: center; width: 100px"><a href="/admin/AddNews" ><img src="/content/images/news_add.png" alt="Add News" /><br />Add news</a></div>
<div style="margin-left: 170px; text-align: center; width: 100px"><a href="/admin/AllNews" ><img src="/content/images/news_all.png" alt="All news" /><br />All news</a></div>


<h2>Add news</h2>

@if (ViewBag.Message != null)
{
    @ViewBag.Message
}
else
{
    if (ViewBag.Categories != null)
    {
        if (ViewBag.Tags != null)
        {
            using (Html.BeginForm("AddNews", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {


        @Html.ValidationSummary()

        <table id="table_admin">
            <tr>
                <td>
                    @Html.LabelFor(x => x.Title)
                </td>
                <td>
                    @Html.TextBoxFor(x => x.Title) 
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(x => x.Text)
                </td>
                <td>
                    @Html.TextAreaFor(x => x.Text)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(x => x.Category)
                </td>
                <td>
                    @{
                List<Category> category = ViewBag.Categories;
                        @Html.DropDownListFor(x => x.Category, new SelectList(category, "Id", "Name"))
                    }  
                </td>
            </tr>
            <tr>
                <td>
                    <label>Tags
                    </label>
                </td>
                <td>
                    

                    @{
                        List<Tag> tags = ViewBag.Tags;
                        
                    }
                    
                    <ul id="sortable1" class="otherlist">
                        @foreach (Tag tag in tags)
                        {
                            <li id="@tag.Id" class="ui-state-default">@tag.Name</li>
                        }
                    </ul>
                   
                    <ul id="sortable2" class="otherlist" >
                       
                    </ul>

                
                
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div id="filesToUpload">
                    <input type="file" name="uploadFiles"/> <br/>

                    </div>
                    <a href="#" onclick="return addField()">Add fields</a>
                </td>
            </tr>
        </table>



        @Html.Hidden("Author", auth.Name)
        @Html.Hidden("tags","")
        <br />
        <input type="submit" name="addNewsSubm" value="Add" /><!--onclick="return  sub()"-->
            }
        }
        else
        {
            @Html.ActionLink("No tags! PLZ ADD","AddTag","Admin")
        }
    }
    else
    {
        <span>@Html.ActionLink("NO category! PLZ ADD","AddCategory","Admin")</span>
    }
}